package pro.cherish.knowledgeobjectstorage.config.storageConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pro.cherish.knowledgeobjectstorage.core.LocalStorage;
import pro.cherish.knowledgeobjectstorage.core.MonioStorage;
import pro.cherish.knowledgeobjectstorage.core.QiniuStorage;
import pro.cherish.knowledgeobjectstorage.core.StorageService;

@EnableConfigurationProperties
@Configuration
public class StorageAutoConfiguration {

  @Autowired private StorageProperties properties;

  /**
   * fixeme 策略模式体现
   *
   * @return
   */
  @Bean
  public StorageService storageService() {
    StorageService storageService = new StorageService();
    String active = this.properties.getActive();
    storageService.setActive(active);
    if (active.equals("local")) {
      storageService.setStorage(localStorage());
    } else if (active.equals("qiniu")) {
      storageService.setStorage(qiniuStorage());
    } else if (active.equals("minio")) {
      storageService.setStorage(minioStorage());
    } else {
      throw new RuntimeException("当前存储模式 " + active + " 不支持");
    }
    return storageService;
  }

  @Bean
  public LocalStorage localStorage() {
    LocalStorage localStorage = new LocalStorage();
    LocalProperties localProperties = this.properties.getLocalProperties();
    localStorage.setAddress(localProperties.getAddress());
    localStorage.setStoragePath(localProperties.getStoragePath());
    return localStorage;
  }

  @Bean
  public QiniuStorage qiniuStorage() {
    QiniuStorage qiniuStorage = new QiniuStorage();
    QiniuProperties qiniuProperties = this.properties.getQiniuProperties();
    qiniuStorage.setAccessKey(qiniuProperties.getAccessKey());
    qiniuStorage.setSecretKey(qiniuProperties.getSecretKey());
    qiniuStorage.setBucketName(qiniuProperties.getBucketName());
    qiniuStorage.setEndpoint(qiniuProperties.getEndpoint());
    return qiniuStorage;
  }

  @Bean
  public MonioStorage minioStorage() {
    MonioStorage minioStorage = new MonioStorage();
    MinioProperties monioProperties = this.properties.getMonioProperties();
    minioStorage.setAccessKey(monioProperties.getAccessKey());
    minioStorage.setSecretKey(monioProperties.getSecretKey());
    minioStorage.setBucketName(monioProperties.getBucketName());
    minioStorage.setEndpoint(monioProperties.getEndpoint());
    return minioStorage;
  }
}
